const express = require('express')
const { nanoid } = require('nanoid')
const { getUserInfo } = require('../utils/permission')
const User = require('../models/User')
const router = express.Router()

/**
 * 根据 token 获取用户的地址数据
 */
router.get('/', (req, res) => {
  const user = getUserInfo(req)
  User
    .find({phone: user?.phone}, 'addresses')
    .then(data => {
      const addresses = data[0]?.addresses ?? []
      res.json({
        code: 200,
        data: {
          addresses
        }
      })
    })
})

/**
 * 获取默认收货地址
 */
 router.get('/default', (req, res) => {
  const user = getUserInfo(req)
  User
    .find({phone: user?.phone}, 'addresses')
    .then(data => {
      const addresses = data[0]?.addresses ?? []
      const defaultAddress = addresses.find(ad => ad.isDefault) ?? addresses[0] ?? null
      res.json({
        code: 200,
        data: {
          defaultAddress
        }
      })
    })
})

/**
 * 添加地址信息
 */
router.post('/', (req, res) => {
  const user = getUserInfo(req)
  if (user) {
    /* 从请求中获取添加的地址数据 */
    const { address } = req.body

    User
      .find({phone: user?.phone}, 'addresses')
      .then(data => {
        const addresses = data[0]?.addresses ?? []
        // 如果当前添加地址为默认，则其它为非默认
        if (address.isDefault) {
          addresses.forEach(ad => ad.isDefault = false)
        }
        address.id = nanoid()
        addresses.push(address)
        User
          .updateOne({phone: user.phone}, {addresses})
          .then(() => {
            res.json({
              code: 200,
              data: {
                message: '添加成功',
                id: address.id,
              }
            })
          })
      })
  } else {
    res.json({
      code: 200,
      data: {
        message: '没有用户信息'
      }
    })
  }
})

/**
 * 修改地址信息
 */
router.put('/:id', (req, res) => {
  const user = getUserInfo(req)
  if (user) {
    const { id } = req.params
    const { address } = req.body
    
    User
      .find({phone: user?.phone}, 'addresses')
      .then(data => {
        const addresses = data[0]?.addresses ?? []

        const result = addresses.map(ad => {
          const copy = { ...ad }
          if (address.isDefault) {
            copy.isDefault = false
          }
          if (copy.id === id) {
            return {id, ...address}
          }
          return copy
        })

        User
          .updateOne({phone: user.phone}, { addresses: result })
          .then(() => {
            res.json({
              code: 200,
              data: {
                message: '更新成功'
              }
            })
          })
      })
  } else {
    res.json({
      code: 200,
      data: {
        message: '没有用户信息'
      }
    })
  }
})

/**
 * 删除地址
 */
router.delete('/:id', (req, res) => {
  const user = getUserInfo(req)
  if (user) {
    const { id } = req.params
    
    User
      .find({phone: user?.phone}, 'addresses')
      .then(data => {
        const addresses = data[0]?.addresses ?? []

        const result = addresses.filter(ad => ad.id !== id)

        User
          .updateOne({phone: user.phone}, { addresses: result })
          .then(() => {
            res.json({
              code: 200,
              data: {
                message: '删除成功'
              }
            })
          })
      })
  } else {
    res.json({
      code: 200,
      data: {
        message: '没有用户信息'
      }
    })
  }
})

module.exports = router
